#include <iostream>
#include "mysql.h"
// https://dev.mysql.com/doc/c-api/8.0/en/c-api-function-reference.html
const std::string host = "127.0.0.1";
const unsigned int port = 8080;
const std::string user = "xl";
const std::string passwd = "";
const std::string db = "test";

int main()
{
    // std::cout << "mysql cilent version: " << mysql_get_client_info() << std::endl;
    MYSQL *mysql = mysql_init(nullptr);
    if (nullptr == mysql)
    {
        std::cerr << "init error" << std::endl;
        return 1;
    }
    if (mysql_real_connect(mysql, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
    {
        std::cerr << "connect error" << std::endl;
        return 2;
    }
    std::cout << "connect success" << std::endl;
    // set the default character set for the current connection
    mysql_set_character_set(mysql, "utf8");
    std::string sql;

    while (1)
    {
        std::cout << "input sql statment: ";
        if (std::getline(std::cin, sql))
        {
            if (sql == "quit")
            {
                std::cout << "Bye" << std::endl;
                break;
            }
            int n = mysql_query(mysql, sql.c_str());
            if (n == 0)
            {
                std::cout << sql << " success" << std::endl;
            }
            else
            {
                std::cout << sql << "failed" << std::endl;
            }
            // show select desc ...
            if (sql.find("select") != std::string::npos || sql.find("show") != std::string::npos)
            //|| sql.find("desc") != std::string::npos)
            {
                // 结果都是字符串
                // 可以看成 char** [] 或 char* [][]
                MYSQL_RES *res = mysql_store_result(mysql);
                if (nullptr == res)
                {
                    std::cerr << "store result faild" << std::endl;
                }
                else
                {
                    // std::cout << "store success" << std::endl;
                    my_ulonglong rows = mysql_num_rows(res);
                    my_ulonglong cols = mysql_num_fields(res);
                    std::cout << rows << "行" << cols << "列" << std::endl;
                    // 获取列名（表头）
                    MYSQL_FIELD *field = mysql_fetch_fields(res);
                    int i = 0;
                    for (; i < cols; i++)
                    {
                        std::cout << field[i].name << "\t";
                    }
                    std::cout << std::endl;
                    i = 0;
                    MYSQL_ROW line;
                    for (; i < rows; i++)
                    {
                        // 每一行的地址拿，放到line中
                        // 类似于迭代器
                        line = mysql_fetch_row(res);
                        int j = 0;
                        // std::cout << line << std::endl;
                        for (; j < cols; j++)
                        {

                            std::cout << line[j] << "\t";
                        }
                        std::cout << std::endl;
                    }
                }
                // 释放空间
                mysql_free_result(res);
            }
        }
        else
            break;
    }

    mysql_close(mysql);
    return 0;
}
